﻿using System;

namespace TK.Common.Tools
{
    /// <summary>
    /// DateTimeConverter
    /// </summary>
    public static class DateTimeTool
    {
        /// <summary>
        /// ToDateTime
        /// </summary>
        public static DateTime ToDateTime(string timeStamp)
        {
            var startTime = new DateTime(1970, 1, 1).ToLocalTime();

            if (long.TryParse(timeStamp, out var createTimeStamp))
            {
                var timeSpan = new TimeSpan(createTimeStamp * 10000000);
                return startTime.Add(timeSpan);
            }

            return startTime;
        }

        /// <summary>
        /// 获取当前时间戳
        /// </summary>
        public static long CurrentTimeStamp()
        {
            var dateTime = DateTime.Now;
            var startTime = new DateTime(1970, 1, 1).ToLocalTime();

            return Convert.ToInt32((dateTime - startTime).TotalSeconds);
        }

        /// <summary>
        /// 获取当天开始时间
        /// </summary>
        public static DateTimeOffset BeginTimeOfDay(this DateTimeOffset time)
        {
            var chinaCoastTime = time.ToChinaCoastTime();
            var beginTimeOfDay = chinaCoastTime.Date;
            return beginTimeOfDay.ToDateTimeOffset();
        }

        /// <summary>
        /// 获取当天结束时间
        /// </summary>
        public static DateTimeOffset EndTimeOfDay(this DateTimeOffset time)
        {
            var chinaCoastTime = time.ToChinaCoastTime();
            var endTimeOfDay = chinaCoastTime.Date.AddDays(1).AddMilliseconds(-1);
            return endTimeOfDay.ToDateTimeOffset();
        }

        /// <summary>
        /// 获取时间（精确到秒）
        /// </summary>
        public static DateTimeOffset ExactTimeOfSeconds(this DateTimeOffset time)
        {
            return DateTimeOffset.FromUnixTimeSeconds(time.ToUnixTimeSeconds());
        }

        /// <summary>
        /// 中国东部时间
        /// </summary>
        public static DateTime ToChinaCoastTime(this DateTimeOffset time)
        {
            return new DateTime(time.UtcDateTime.AddHours(8).Ticks, DateTimeKind.Unspecified);
        }

        /// <summary>
        /// DateTimeOffset时间
        /// </summary>
        public static DateTimeOffset ToDateTimeOffset(this DateTime time)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(time.AddHours(-8), TimeZoneInfo.Local);
        }

        /// <summary>
        /// ToDateTimeOffset
        /// </summary>
        public static DateTimeOffset ToDateTimeOffset(string timeStamp)
        {
            if (long.TryParse(timeStamp, out var createTimeStamp))
            {
                return DateTimeOffset.FromUnixTimeSeconds(createTimeStamp);
            }

            return DateTimeOffset.MinValue;
        }

        /// <summary>
        /// 获取传入时间一年有效时间范围，返回->开始时间，结束时间
        /// </summary>
        public static ValueTuple<DateTimeOffset, DateTimeOffset> GetDateRangeOfYear(DateTimeOffset time)
        {
            var chinaTime = time.ToChinaCoastTime();

            var startTime = new DateTime(chinaTime.Year, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).ToDateTimeOffset();
            var endTime = startTime.AddYears(1).AddMilliseconds(-1);

            return new ValueTuple<DateTimeOffset, DateTimeOffset>(startTime, endTime);
        }

        /// <summary>
        /// Unix 时间戳 转 DateTimeOffset
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static DateTimeOffset ToDateTime(this long source)
        {
            var dateTimeStart = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.FromHours(0));
            dateTimeStart.AddSeconds(source);

            return dateTimeStart.AddSeconds(source).ToOffset(TimeSpan.FromHours(8));
        }

        /// <summary>
        /// DateTime 时间格式转换为 Unix 时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static ulong ToStamp(this DateTimeOffset time)
        {
            var startTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.FromHours(0));
            startTime = startTime.ToOffset(TimeSpan.FromHours(8));
            return (ulong)(time - startTime).TotalSeconds;
        }
    }
}